import axios from 'axios';
// 导入路由实例对象
// import router from '@/router'
// 引入element-plus组件库的样式文件
import 'element-plus/dist/index.css'
import { ElLoading, type LoadingParentElement } from 'element-plus'
import type { ComponentPublicInstance, ComponentOptionsBase, ComponentProvideOptions, Ref } from 'vue';
import { Md5 } from "ts-md5";
import globalConfig from "../../../global.config.ts";
const apiBaseUrl = import.meta.env.VITE_API_URL;
console.log(import.meta.env);
axios.create({
    // baseURL: 'http://localhost:3032/',
    //检测当前为 什么环境       // 当前为开发环境 则使用本地地址
    // baseURL: import.meta.env.MODE === 'development' ? 'http://localhost:3032/' : '线上',
    baseURL: apiBaseUrl,
    timeout: 3000,
    headers: {
        "Content-Type": "application/json",
    }
});
// 声明一个全局变量
// eslint-disable-next-line no-var
var loadingInstance: {
    close: any;
    setText?: (text: string) => void;
    removeElLoadingChild?: () => void;
    handleAfterLeave?: () => void;
    vm?: ComponentPublicInstance<{}, {}, {}, {}, {}, {}, {}, {}, false, ComponentOptionsBase<any, any, any, any, any, any, any, any, any, {}, {}, string, {}, {}, {}, string, ComponentProvideOptions>, {}, {}, "", {}, any>;
    $el?: HTMLElement; originalPosition?: Ref<string, string>
    originalOverflow?: Ref<string, string>
    visible?: Ref<boolean, boolean>
    parent?: Ref<LoadingParentElement, LoadingParentElement>
    background?: Ref<string, string>
    svg?: Ref<string, string>
    svgViewBox?: Ref<string, string>
    spinner?: Ref<string | boolean, string | boolean>
    text?: Ref<string, string>
    fullscreen?: Ref<boolean, boolean>
    lock?: Ref<boolean, boolean>
    customClass?: Ref<string, string>
    target?: Ref<HTMLElement, HTMLElement>
    beforeClose?: Ref<(() => boolean) | undefined, (() => boolean) | undefined> | undefined
    closed?: Ref<(() => void) | undefined, (() => void) | undefined> | undefined

}

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    // 请求白名单
    let whiteList = globalConfig.whiteListApi;
    // 请求url
    let url = config.url as string;
    // 获取token
    const token = localStorage.getItem('token');
    if (whiteList.indexOf(url) !== -1 && token) {
        config.headers["token"] = token as string;
    }
    // 密钥  secretId + md5加密
    let _secret = Md5.hashStr(globalConfig.secretId + new Date().toString())
    // 通过headers传递密钥
    config.headers.secretId = _secret;
    // if (token) {
    //     config.headers.Authorization = 'Bearer ' + token;
    // }
    // 显示loading效果
    // loadingInstance = ElLoading.service()
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    return response;
}, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    // 关闭loading效果
    // setTimeout(() => {
    //     loadingInstance.close()
    // }, 1500);
    if (error.response.status === 200) {
        console.log('成功进入!!!');
    } else if (error.response.status === 401) {
        console.log('未授权!!!,请联系管理员刘浩QQ499735432');
        window.location.href = '/login';
    } else if (error.response.status === 400) {
        console.log('错误请求!!!,请联系管理员刘浩QQ499735432');
    } else if (error.response.status === 403) {
        console.log('禁止!!!,请联系管理员刘浩QQ499735432');
    } else if (error.response.status === 404) {
        console.log('未找到!!!,请联系管理员刘浩QQ499735432');
    } else if (error.response.status === 405) {
        console.log('方法禁用!!!,请联系管理员刘浩QQ499735432');
    } else if (error.response.status === 406) {
        console.log('不接受!!!,请联系管理员刘浩QQ499735432');
    } else if (error.response.status === 408) {
        console.log('请求超时!!!,请联系管理员刘浩QQ499735432');
    } else if (error.response.status === 500) {
        console.log('服务器错误!!!,请联系管理员刘浩QQ499735432');
    } else if (error.response.status === 502) {
        console.log('错误网关!!!,请联系管理员刘浩QQ499735432');
    } else if (error.response.status === 503) {
        console.log('服务不可用!!!,请联系管理员刘浩QQ499735432');
    } else if (error.response.status === 504) {
        console.log('网关超时!!!,请联系管理员刘浩QQ499735432');
    } else {
        console.log('警告!!!出现未知错误,请联系管理员刘浩QQ499735432');
    }
    return Promise.reject(error);
});
export default axios;